package com.ebt.m.customer.util;

import android.animation.Animator;
import android.animation.ValueAnimator;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.RecyclerView.LayoutManager;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;

import com.ebt.m.customer.constant.CustomerConstant;

import java.lang.ref.WeakReference;

/**
 * Created by Rick.Wang on 2016/4/26.
 */
public class DrawerManager <T extends ViewGroup>{

    private static final String TAG = "DrawerManager";
    //TODO 连续点击的问题没有处理
    private static final int DEFAULT_POSITION = -1;
    private static final int DEFAULT_SIZE = 0;

    private WeakReference<T> wr;
    private int prevCheckedPosition = DEFAULT_POSITION;

    private int itemWidth = DEFAULT_SIZE;
    private int itemHeight = DEFAULT_SIZE;

    public DrawerManager(T viewgroup) {
        wr = new WeakReference<T>(viewgroup);
    }

    public void setSize(int itemWidth, int itemHeight) {
        if (this.itemWidth != itemWidth) {
            this.itemWidth = itemWidth;
        }
        if (this.itemHeight != itemHeight) {
            this.itemHeight = itemHeight;
        }
    }

    //根据数据记录状况确定drawer是否展开
    public void correctView(View drawer, int position) {
        if (position == this.prevCheckedPosition) {
            restoreDrawer(drawer);
        } else {
            resetDrawer(drawer);
        }
    }

    public void triggerView(View drawer, int position, int drawerViewId) {
        if (position == this.prevCheckedPosition) {
            hideDrawer(drawer);
        } else {
            if (prevCheckedPosition == DEFAULT_POSITION) {
                //没有展开的,donothing
            } else {
                //有展开的
                hideTheOther(wr, drawerViewId);//R.id.menu_drawer
            }
            showDrawer(drawer, position);
        }
    }

    public void hideTheOther(WeakReference<T> wr, int drawerID) {
        T viewgroup = wr.get();
        if(viewgroup != null) {
            int firstVisiblePosition = -1;
            int lastVisiblePosition = -1;
            if (viewgroup instanceof ListView) {
                firstVisiblePosition = ((ListView) viewgroup).getFirstVisiblePosition();
                lastVisiblePosition = ((ListView) viewgroup).getLastVisiblePosition();
            }else if(viewgroup instanceof RecyclerView) {
                LayoutManager layoutmanager = ((RecyclerView)viewgroup).getLayoutManager();
                if(layoutmanager instanceof LinearLayoutManager){
                    firstVisiblePosition = ((LinearLayoutManager)layoutmanager).findFirstVisibleItemPosition();
                    lastVisiblePosition = ((LinearLayoutManager)layoutmanager).findLastVisibleItemPosition();
                }else if(layoutmanager instanceof GridLayoutManager){
                    firstVisiblePosition = ((GridLayoutManager)layoutmanager).findFirstVisibleItemPosition();
                    lastVisiblePosition = ((GridLayoutManager)layoutmanager).findLastVisibleItemPosition();
                }
            }
            if(firstVisiblePosition > -1 && lastVisiblePosition > -1) {
                if (prevCheckedPosition != DEFAULT_POSITION) {
                    if (prevCheckedPosition < firstVisiblePosition || prevCheckedPosition > lastVisiblePosition) {
                        //这个不在当前显示
                        prevCheckedPosition = DEFAULT_POSITION;
                    } else {
                        View itemview = viewgroup.getChildAt((prevCheckedPosition - firstVisiblePosition));
                        View drawer = itemview.findViewById(drawerID);
                        if (drawer.getVisibility() != View.GONE) {
                            hideDrawer(drawer);
                        }
                    }
                }
            }
        }
    }

    //具有动效的显示
    public void showDrawer(View drawer, int position) {
        prevCheckedPosition = position;
        ViewGroup.MarginLayoutParams params = (ViewGroup.MarginLayoutParams) drawer.getLayoutParams();
        params.width = itemWidth;
        params.height = itemHeight;
        drawer.setLayoutParams(params);

        ValueAnimator va = createAnimator(drawer, itemWidth, 0);
        drawer.setVisibility(View.VISIBLE);
        va.start();
    }

    //具有动效的隐藏
    public void hideDrawer(View drawer) {
        prevCheckedPosition = DEFAULT_POSITION;
        ValueAnimator va = createAnimator(drawer, 0, itemWidth);
        va.start();
    }

    //getView时显示，没有动效
    private void restoreDrawer(View view) {
        view.setVisibility(View.VISIBLE);
    }

    //getView时隐藏，没有动效
    private void resetDrawer(View view) {
        view.setVisibility(View.GONE);
    }

    public ValueAnimator createAnimator(final View drawer, final int start, final int end) {
        ValueAnimator animator = ValueAnimator.ofInt(start, end);
        animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator valueAnimator) {
                int value = (Integer) valueAnimator.getAnimatedValue();
                drawer.setTranslationX(value);
            }
        });
        animator.addListener(new Animator.AnimatorListener() {

            @Override
            public void onAnimationEnd(Animator a) {
                if (start < end) {//隐藏
                    drawer.setVisibility(View.GONE);
                }
            }

            @Override
            public void onAnimationStart(Animator a) {
            }

            @Override
            public void onAnimationRepeat(Animator a) {
            }

            @Override
            public void onAnimationCancel(Animator a) {
            }
        });

        animator.setDuration(CustomerConstant.CUSTOMER_ANIMATION_OPTION_MENU_DURATION);
        return animator;
    }

}